package com.example.emos.workflow.bpmn;

import cn.hutool.core.map.MapUtil;
import com.example.emos.workflow.db.dao.TbReimDao;
import com.example.emos.workflow.db.dao.TbUserDao;
import com.example.emos.workflow.exception.EmosException;

import com.example.emos.workflow.service.ReimService;
import com.example.emos.workflow.task.EmailTask;
import com.example.emos.workflow.task.SendEmailTask;
import org.activiti.engine.HistoryService;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.JavaDelegate;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.history.HistoricTaskInstanceQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@Component("notifyReimService")
public class NotifyReimService implements JavaDelegate {

    @Autowired
    private HistoryService historyService;

    @Autowired
    private ReimService reimService;

    @Autowired
    private TbReimDao reimDao;

    @Autowired
    private SendEmailTask sendEmailTask;


    @Override
    public void execute(DelegateExecution delegateExecution) {

//        HistoricTaskInstance taskInstance = historyService.createHistoricTaskInstanceQuery().includeProcessVariables()
//                .includeTaskLocalVariables().processInstanceId(delegateExecution.getProcessInstanceId())
//                .orderByHistoricTaskInstanceEndTime().singleResult();
//        String result = taskInstance.getTaskLocalVariables().get("result").toString();

        HistoricTaskInstanceQuery taskQuery = historyService.createHistoricTaskInstanceQuery()
                .orderByHistoricTaskInstanceStartTime().desc()
                .includeTaskLocalVariables().includeProcessVariables()
                .processInstanceId(delegateExecution.getProcessInstanceId());

        HistoricTaskInstance taskInstance = taskQuery.list().get(0);
        String result = taskInstance.getTaskLocalVariables().get("result").toString();

        delegateExecution.setVariable("result", result);
        delegateExecution.setVariable("filing",true);
        String instanceId = delegateExecution.getProcessInstanceId();
        HashMap param = new HashMap() {{
            put("status", "同意".equals(result) ? 3 : 2);
            put("instanceId", instanceId);
        }};

        int rows = reimDao.updateReimStatus(param);
        if (rows != 1) {
            throw new EmosException("更新报销记录状态失败");
        } else {
            //Map<String, Object> processVariables = taskInstance.getProcessVariables();
            Map<String, Object> map = taskInstance.getTaskLocalVariables();

            HashMap reimMap = reimService.searchReimByInstanceId(instanceId);
            String createTime =MapUtil.getStr(reimMap,"createTime");
            map.put("createTime",createTime);
            sendEmailTask.SendEmailToApproval(map);


        }


    }
}
